package com.uum.admin.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uum.admin.dto.OAuth2ClientDTO;
import com.uum.admin.pojo.entity.SysOauthClient;
import com.uum.admin.pojo.query.ClientPageQuery;
import com.uum.admin.pojo.query.UserPageQuery;
import com.uum.admin.service.SysOauthClientService;
import com.uum.common.core.annotation.Operation;
import com.uum.common.core.enums.BusinessType;
import com.uum.common.core.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@Api(tags = "客户端接口")
@RestController
@RequestMapping("/api/v1/oauth-clients")
public class SysOauthClientController {

    @Autowired
    private SysOauthClientService sysOauthClientService;

    @Operation(description = "查询客户端列表",businessType = BusinessType.QUERY,isSaveRequestData = false)
    @ApiOperation(value = "客户端列表分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "页码", paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "clientId", value = "客户端ID", paramType = "query", dataType = "String")
    })
    @GetMapping("page")
    public Result pageClient(ClientPageQuery clientPageQuery) {
        IPage<SysOauthClient> result = sysOauthClientService.page(
                new Page<>(clientPageQuery.getCurrentPage(), clientPageQuery.getPageSize()),
                new LambdaQueryWrapper<SysOauthClient>()
                        .like(StrUtil.isNotBlank(clientPageQuery.getClientId()), SysOauthClient::getClientId, clientPageQuery.getClientId()));
        return Result.success(result.getRecords(), result.getTotal());
    }


    @ApiOperation(value = "客户端详情")
    @ApiImplicitParam(name = "clientId", value = "客户端id", required = true, paramType = "path", dataType = "String")
    @GetMapping("detail/{clientId}")
    public Result clientDetail(@PathVariable String clientId) {
        SysOauthClient client = sysOauthClientService.getById(clientId);
        return Result.success(client);
    }

    @Operation(description = "新增客户端",businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增客户端")
    @ApiImplicitParam(name = "client", value = "实体JSON对象", required = true, paramType = "body", dataType = "OauthClientDetails")
    @PostMapping("add")
    public Result addClient(@RequestBody SysOauthClient client) {
        boolean status = sysOauthClientService.save(client);
        return Result.judge(status);
    }

    @Operation(description = "修改客户端",businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改客户端")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "clientId", value = "客户端id", required = true, paramType = "path", dataType = "String"),
            @ApiImplicitParam(name = "client", value = "实体JSON对象", required = true, paramType = "body", dataType = "OauthClientDetails")
    })
    @PutMapping(value = "update")
    public Result updateClient(@RequestBody SysOauthClient client) {
        boolean status = sysOauthClientService.updateById(client);
        return Result.judge(status);
    }

    @Operation(description = "删除客户端",businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除客户端")
    @ApiImplicitParam(name = "ids", value = "id集合,以,拼接字符串", required = true, paramType = "query", dataType = "String")
    @DeleteMapping("delete/{ids}")
    public Result deleteClient(@PathVariable("ids") String ids) {
        boolean status = sysOauthClientService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.judge(status);
    }

    @ApiOperation(hidden = true, value = "获取 OAuth2 客户端认证信息", notes = "Feign 调用")
    @GetMapping("getOAuth2ClientById")
    public Result<OAuth2ClientDTO> getOAuth2ClientById(@RequestParam String clientId) {
        SysOauthClient client = sysOauthClientService.getById(clientId);
        Assert.isTrue(client!=null, "OAuth2 客户端不存在");
        OAuth2ClientDTO oAuth2ClientDTO = new OAuth2ClientDTO();
        BeanUtil.copyProperties(client, oAuth2ClientDTO);
        return Result.success(oAuth2ClientDTO);
    }
}

